<script setup lang="ts">
import { ref, onMounted } from 'vue'
import {
  reqSkuList,
  reqCancelSale,
  reqSkuInfo,
  reqRemoveSku,
} from '@/api/product/sku/index'
import { ElMessage } from 'element-plus'
// import { SkuData } from '@/api/product/spu/type'
let pageSize = ref(4)
//当前默认展示页码
let currentPage = ref(1)
let total = ref<number>(10)
//显示的sku数组
let skuArr = ref<any>([])
// 抽屉显示与否
const drawer = ref(false)

let skuInfo = ref<any>({})
onMounted(() => {
  getSkuList()
})

// 页码发生变化
// const currentPageChange = (page: any) => {
//   // console.log('分页器变化', page)

// }
let getSkuList = async (pager = 1) => {
  currentPage.value = pager
  let res = await reqSkuList(currentPage.value, pageSize.value)
  console.log(res)
  if (res.code == 200) {
    skuArr.value = res.data.records
    total.value = res.data.total
  }
}
//分页器每页的size改变
const sizeChange = () => {
  // pageSize.value = pageSize
  getSkuList()
}

//更新商品状态 上架/下架
const updateSale = async (row: any) => {
  //上架情况 1->原先是上架，现在变为下架
  if (row.isSale == 1) {
    await reqCancelSale(row.id)
    // console.log(res)
    ElMessage({
      type: 'error',
      message: '下架成功',
    })
    getSkuList()
  }
  //下架
  else {
    await reqCancelSale(row.id)
    // console.log(res)
    ElMessage({
      type: 'success',
      message: '上架成功',
    })
    getSkuList()
  }
}
const updateSku = () => {
  ElMessage({
    type: 'success',
    message: '敬请期待',
  })
}
//查看按钮
const viewSku = async (row: any) => {
  drawer.value = true
  let res: any = await reqSkuInfo(row.id)
  // console.log(res)
  // console.log(res.data)
  // console.log(res.code)

  // console.log(result)
  if (res.code == 200) {
    skuInfo.value = res.data
    // console.log(skuInfo, 'skuInfoValue')
  }
}

// 删除sku商品
const removeSku = async (id: number) => {
  let res: any = await reqRemoveSku(id)
  if (res.code == 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    getSkuList(
      skuArr.value.length > 1 ? currentPage.value : currentPage.value - 1,
    )
  }
}
</script>

<template>
  <el-card>
    <el-table border style="margin: 10px 0px" :data="skuArr">
      <el-table-column
        label="序号"
        type="index"
        align="center"
        width="80px"
      ></el-table-column>
      <el-table-column
        label="名称"
        show-overflow-tooltip
        width="150px"
        prop="skuName"
      ></el-table-column>
      <el-table-column
        label="描述"
        show-overflow-tooltip
        width="150px"
        prop="skuDesc"
      ></el-table-column>
      <el-table-column label="图片" width="150px">
        <template #="{ row, $index }">
          <img
            :src="row.skuDefaultImg"
            alt=""
            style="width: 100px; height: 100px"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="重量"
        width="150px"
        prop="weight"
      ></el-table-column>
      <el-table-column
        label="价格"
        width="150px"
        prop="price"
      ></el-table-column>
      <el-table-column label="操作" width="250px" fixed="right">
        <template #="{ row, $index }">
          <el-button
            type="primary"
            size="small"
            :icon="row.isSale == 1 ? 'Bottom' : 'Top'"
            @click="updateSale(row)"
          ></el-button>
          <el-button
            type="primary"
            size="small"
            icon="Edit"
            @click="updateSku"
          ></el-button>
          <el-button
            type="primary"
            size="small"
            icon="InfoFilled"
            @click="viewSku(row)"
          ></el-button>
          <el-popconfirm
            :title="`你确定要删除${row.skuName}?`"
            width="200px"
            @confirm="removeSku(row.id)"
          >
            <template #reference>
              <el-button type="primary" size="small" icon="Delete"></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[2, 4, 6, 8]"
      background
      layout="prev, pager, next, jumper, -> ,total, sizes"
      :total="total"
      @current-change="getSkuList"
      @size-change="sizeChange"
    />
    <!-- 抽屉组件:展示商品详情 -->
    <el-drawer v-model="drawer">
      <template #header>
        <h4>查看商品的详情</h4>
      </template>
      <template #default>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">名称</el-col>
          <el-col :span="18">{{ skuInfo.skuName }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">描述</el-col>
          <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">价格</el-col>
          <el-col :span="18">{{ skuInfo.price }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">平台属性</el-col>
          <el-col :span="18">
            <el-tag
              style="margin: 5px"
              v-for="item in skuInfo.skuAttrValueList"
              :key="item.id"
            >
              {{ item.valueName }}
            </el-tag>
          </el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">销售属性</el-col>
          <el-col :span="18">
            <el-tag
              style="margin: 5px"
              v-for="item in skuInfo.skuSaleAttrValueList"
              :key="item.id"
            >
              {{ item.saleAttrValueName }}
            </el-tag>
          </el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">商品图片</el-col>
          <el-col :span="18">
            <el-carousel :interval="4000" type="card" height="200px">
              <el-carousel-item
                v-for="item in skuInfo.skuImageList"
                :key="item.id"
              >
                <img
                  :src="item.imgUrl"
                  alt=""
                  style="width: 100%; height: 100%"
                />
              </el-carousel-item>
            </el-carousel>
          </el-col>
        </el-row>
      </template>
    </el-drawer>
  </el-card>
</template>

<style scoped></style>
